package com.webank.wedatasphere.qualitis.question.controller;

import com.webank.wedatasphere.qualitis.controller.request.PageReqDto;
import com.webank.wedatasphere.qualitis.question.entity.Order;
import com.webank.wedatasphere.qualitis.question.service.OrderService;
import com.webank.wedatasphere.qualitis.question.service.dto.AssignDto;
import com.webank.wedatasphere.qualitis.question.service.dto.UserDto;
import com.webank.wedatasphere.qualitis.response.Grid;
import com.webank.wedatasphere.qualitis.response.Resp;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 问题工单
 */
@RestController
@RequestMapping("/question/order")
public class OrderController {
    @Resource
    private OrderService orderService;

    @RequestMapping("/datagrid")
    public Grid<Order> dataGrid(Order order, PageReqDto dto) {
        return orderService.dataGrid(order, dto);
    }

    @RequestMapping("/save")
    public Resp<String> save(@RequestBody Order order) {
        Order save = orderService.save(order);
        if (save != null && save.getId() != null) {
            return Resp.Ok();
        } else {
            return Resp.error("保存失败，请检查工单数据！");
        }
    }

    @PostMapping("/close")
    public Resp<String> close(@RequestBody List<Long> ids) {
        int closed = orderService.close(ids);
        if (closed == ids.size()) {
            return Resp.Ok();
        } else {
            return Resp.error("关闭失败，请检查工单状态！");
        }
    }

    @PostMapping("/assign")
    public Resp<String> assign(@RequestBody AssignDto dto) {
        int assigned = orderService.assign(dto.getIds(), dto.getHandler());
        if (assigned == dto.getIds().size()) {
            return Resp.Ok();
        } else {
            return Resp.error("分配失败，请检查工单状态！");
        }
    }

    @PostMapping("/return")
    public Resp<String> doReturn(@RequestBody List<Long> ids) {
        int assigned = orderService.doReturn(ids);
        if (assigned == ids.size()) {
            return Resp.Ok();
        } else {
            return Resp.error("打回失败，请检查工单状态！");
        }
    }

    @GetMapping("/user/list")
    public Resp<List<UserDto>> getOrderUsers(){
        return Resp.Ok(orderService.getOrderUsers());
    }

}
